package com.example.dao.imp;


import com.example.dao.CommentDao;
import com.example.domain.Comment;
import com.example.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CommentDaoImp implements CommentDao {

    @Override
    public Comment getCommentById(Integer id) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectOne("com.example.dao.CommentDao.getCommentById", id);
        }
    }

    @Override
    public void addComment(Comment comment) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            sqlSession.insert("com.example.dao.CommentDao.addComment", comment);
            sqlSession.commit();
        }
    }

    @Override
    public void deleteComment(Integer id) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            sqlSession.delete("com.example.dao.CommentDao.deleteComment", id);
            sqlSession.commit();
        }
    }

    @Override
    public List<Comment> getCommentsByPostId(Integer postId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.CommentDao.getCommentsByPostId", postId);
        }
    }
}
